using Godot;
using System;
using System.Linq;

public partial class RobotInfo : Node2D
{

    PackedScene buffPrefab = ResourceLoader.Load<PackedScene>(Path.BUFF_UI_SCENE);

    [Signal]
    public delegate void OnRobotCommandEventHandler(string cmd);

    static RobotManager RobotManager => GameManager.Instance.LevelManager.RobotManager;

    #region 子组件
    Control anchor;
    TextureRect icon;
    Label robotName;
    Label robotLevel;
    Label robotAttack;
    Label robotDefense;
    Label robotSpeed;
    Label robotMove;
    Label robotType;
    ProgressBar robotBar;
    Label robotHealth;
    Label RobotMaxHealth;

    TextureRect curPilotAvatar;
    Label curPilotName;
    Label curRobotSprite;
    Label curRobotMaxSprite;
    ProgressBar curPilotBar;
    Button curRobotDetail;


    PanelContainer controlPanel;
    UIButton buttonMove;
    UIButton buttonUndo;
    UIButton buttonLoad;
    UIButton buttonUnLoad;
    UIButton buttonAttack;
    UIButton buttonSkill;
    UIButton buttonEnd;

    FlowContainer buffs;
    #endregion

    void GetChildComponents()
    {
        anchor = GetNode<Control>("%ButtomAnchor");
        icon = GetNode<TextureRect>("%CurRobotIcon");
        robotName = GetNode<Label>("%CurRobotName");
        robotLevel = GetNode<Label>("%CurRobotLevel");
        robotAttack = GetNode<Label>("%CurRobotAttack");
        robotDefense = GetNode<Label>("%CurRobotDefense");
        robotSpeed = GetNode<Label>("%CurRobotSpeed");
        robotMove = GetNode<Label>("%CurRobotMove");
        robotType = GetNode<Label>("%CurRobotType");
        robotBar = GetNode<ProgressBar>("%CurRobotHealthBar");
        robotHealth = GetNode<Label>("%CurRobotHealth");
        RobotMaxHealth = GetNode<Label>("%CurRobotMaxHealth");

        curPilotAvatar = GetNode<TextureRect>("%CurPilotAvatar");
        curPilotName = GetNode<Label>("%CurPilotName");
        curRobotSprite = GetNode<Label>("%CurRobotSprite");
        curRobotMaxSprite = GetNode<Label>("%CurRobotMaxSprite");
        curPilotBar = GetNode<ProgressBar>("%CurPilotBar");
        curRobotDetail = GetNode<Button>("%CurRobotDetail");
        curRobotDetail.Pressed += () => EmitSignal(SignalName.OnRobotCommand, RobotUICommand.Detail.ToString());

        controlPanel = GetNode<PanelContainer>("%ControlPanel");
        buttonMove = GetNode<UIButton>("%Move");
        buttonMove.OnPressed += OnUIButtonPressedHandler;
        buttonUndo = GetNode<UIButton>("%Undo");
        buttonUndo.OnPressed += OnUIButtonPressedHandler;
        buttonLoad = GetNode<UIButton>("%Load");
        buttonLoad.OnPressed += OnUIButtonPressedHandler;
        buttonUnLoad = GetNode<UIButton>("%UnLoad");
        buttonUnLoad.OnPressed += OnUIButtonPressedHandler;
        buttonAttack = GetNode<UIButton>("%Attack");
        buttonAttack.OnPressed += OnUIButtonPressedHandler;
        buttonSkill = GetNode<UIButton>("%Skill");
        buttonSkill.OnPressed += OnUIButtonPressedHandler;
        buttonEnd = GetNode<UIButton>("%End");
        buttonEnd.OnPressed += OnUIButtonPressedHandler;

        buffs = GetNode<FlowContainer>("%Buffs");
    }

    private void OnUIButtonPressedHandler(UIButton button)
    {
        CurrentCommnad = button.CommandString;
        EmitSignal(SignalName.OnRobotCommand, button.CommandString);
        RefreshControls();
    }


    public override void _Ready()
    {
        GetChildComponents();
    }

    public void SetRobotInfo(Robot robot)
    {
        icon.Texture = GameUtil.GetRobotIcon(robot.Entity.Icon, robot.IsPlayer, robot.IsAlly);
        robotName.Text = robot.Entity.RobotName;
        robotLevel.Text = robot.Entity.Level.ToString();
        robotAttack.Text = robot.Entity.TotalAttack.ToString();
        robotDefense.Text = robot.Entity.TotalDefense.ToString();
        robotSpeed.Text = robot.Entity.TotalSpeed.ToString();
        robotMove.Text = robot.Entity.TotalMovement.ToString();
        robotType.Text = GameUtil.TransRobotType(robot.Entity.MoveType);
        robotBar.Value = robot.Entity.CurrentHealth / (float)robot.Entity.TotalHealth;
        robotHealth.Text = robot.Entity.CurrentHealth.ToString();
        RobotMaxHealth.Text = robot.Entity.TotalHealth.ToString();

        curPilotAvatar.Texture = GameUtil.GetPilotAvatar(robot.Entity.Pilot.Avatar);
        curPilotName.Text = robot.Entity.Pilot.PilotName;
        curRobotSprite.Text = robot.Entity.CurrentSprite.ToString();
        curRobotMaxSprite.Text = robot.Entity.TotalSprite.ToString();
        curPilotBar.Value = robot.Entity.CurrentSprite / (float)robot.Entity.TotalSprite;

        if (robot.IsPlayer || robot.IsAlly)
        {
            RefreshControls();
        }
        else
        {
            controlPanel.Visible = false;
        }

        // buffs
        if (robot.Entity.BuffList.Count > 0)
        {
            buffs.Show();
            var buffList = robot.Entity.BuffList;
            var children = buffs.GetChildren();
            var diff = buffList.Count - children.Count;
            int i = 0;
            foreach (var item in children.Cast<Buff>().ToList())
            {
                if (i < buffList.Count)
                {
                    item.Init(buffList[i]);
                    item.Show();
                    i++;
                }
                else
                {
                    item.Hide();
                }
            }
            if (diff > 0)
            {
                for (int j = 0; j < diff; j++)
                {
                    var buff = buffPrefab.Instantiate<Buff>();
                    buffs.AddChild(buff);
                    buff.Init(buffList[i]);
                    buff.Show();
                    i++;
                }
            }
        }
        else
        {
            buffs.Hide();
        }
    }
    string CurrentCommnad;
    public void RefreshControls()
    {
        var robot = GameManager.Instance.LevelManager.CurrentRobot;
        controlPanel.Visible = robot.IsPlayer && (!robot.IsActionEnd || robot.LoadedRobotList.Count > 0);
        if (controlPanel.Visible)
        {

            // 检测是否和母舰在同一个位置
            bool b = RobotManager.IsSamePosAtMotherShip(robot);
            if (b)
            {
                buttonMove.Visible = false;
                buttonUndo.Visible = true;
                buttonLoad.Visible = true;
                buttonAttack.Visible = false;
                buttonSkill.Visible = false;
                buttonEnd.Visible = false;
            }
            else
            {
                buttonMove.Visible = !robot.IsMoveEnd && CurrentCommnad != "Move";
                buttonUndo.Visible = CurrentCommnad != "Undo" && CurrentCommnad != "None";
                buttonAttack.Visible = !robot.IsActionEnd;
                buttonLoad.Visible = false;
                buttonSkill.Visible = !robot.IsActionEnd;
                buttonEnd.Visible = !robot.IsActionEnd;
            }
            buttonUnLoad.Visible = robot.Entity.IsMotherShip && robot.LoadedRobotList.Any(v => !v.IsActionEnd);
        }
    }


    Tween tween;
    Vector2 startPos = new(0, 648);
    Vector2 endPos = new(0, 948);

    public bool Interception => anchor.Position.Y < endPos.Y;

    public void AnimateShow(Robot robot)
    {
        tween?.Stop();
        SetRobotInfo(robot);
        tween = CreateTween();
        tween.TweenProperty(anchor, "position", startPos, 0.2f);
    }

    public void AnimateHide()
    {
        tween?.Stop();
        tween = CreateTween();
        tween.TweenProperty(anchor, "position", endPos, 0.2f);
    }

    internal void Reset()
    {
        CurrentCommnad = RobotUICommand.None.ToString();
    }


    public override void _PhysicsProcess(double delta)
    {
        var robot = GameManager.Instance.LevelManager.CurrentRobot;
        if (Interception && robot != null)
            SetRobotInfo(robot);
    }
}
